﻿Imports FinanceiroDAL
Imports FinanceiroBLL
Imports System.Text.RegularExpressions
Public Class CaixaFRM
    Dim objBLL As New CaixaBLL
    Dim objItBLL As New itensCaixaBLL
    Dim tabItens, tab As New DataTable
    Public Op As String
    Public codigo As Integer
#Region "CARREGA CAMPOS"
    Sub CarregaCampos()
        Try
            objBLL = New CaixaBLL(codigo)
            TCodigo.Text = objBLL.caicodigo
            LControlCaixa.Text = objBLL.caidescricao
            DTPAbertura.Text = objBLL.dataAbertura
            TValFechamento.Text = objBLL.Fechamento
            TCaixaInicial.Text = objBLL.valorInicial
            TValFechamento.Text = objBLL.Fechamento
            CBUsuario.SelectedValue = objBLL.usuario
            CBStatusCx.SelectedValue = objBLL.status
            TSuplementos.Text = objBLL.SomaSuplemento().ToString 'soma suplementos
            TSangria.Text = objBLL.SomaSangria().ToString  'soma sangria
            TVendas.Text = objBLL.SomaVendas().ToString  'soma vendas
            TValordoCAixa.Text = (Decimal.Parse(TCaixaInicial.Text) + Decimal.Parse(TSuplementos.Text) + Decimal.Parse(TVendas.Text)) - Decimal.Parse(TSangria.Text)
            TXTRes.Text = objBLL.res
        Catch ex As Exception
        End Try  '(Trim()).Replace("R", "").Replace("$", "").Replace(" ", "")
    End Sub
#End Region
#Region "CARREGA PROPRIEDADES"
    Sub CarregaPropriedades()
        Try
            objBLL.Limpar()

            If (TCodigo.Text <> "") Then
                objBLL.caicodigo = TCodigo.Text
            Else
                objBLL.caicodigo = 0
            End If
            objBLL.caidescricao = "Caixa 01"
            objBLL.dataAbertura = DTPAbertura.Text
            If (CBStatusCx.SelectedValue = 6) Then
                objBLL.dataFechamento = Date.Now
            Else
                objBLL.dataFechamento = Nothing
            End If

            If (TCaixaInicial.Text <> "") Then
                objBLL.valorInicial = (Trim(TCaixaInicial.Text)).Replace("R", "").Replace("$", "").Replace(" ", "")
            Else
                objBLL.valorInicial = 0
            End If
            If (TValordoCAixa.Text <> "") Then
                objBLL.valorCaixa = (Trim(TValordoCAixa.Text)).Replace("R", "").Replace("$", "").Replace(" ", "")
            Else
                objBLL.valorCaixa = 0
            End If
            If (TValFechamento.Text <> "") Then
                objBLL.Fechamento = (Trim(TValFechamento.Text)).Replace("R", "").Replace("$", "").Replace(" ", "")
            Else
                objBLL.Fechamento = 0
            End If
            objBLL.usuario = CBUsuario.SelectedValue
            objBLL.status = CBStatusCx.SelectedValue

            objBLL.res = TXTRes.Text
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
#End Region
#Region "LIMPAR"
    Sub Limpar()
        TCodigo.Text = Nothing
        TCaixaInicial.Text = ""
        DTPAbertura.Text = Date.Now
        TVendas.Text = ""
        TSuplementos.Text = ""
        TSangria.Text = ""
        TValordoCAixa.Text = ""
        TValFechamento.Text = ""
    End Sub
#End Region
#Region "VISIBLE TRUE"
    Sub operacoesTrue()
        TBValOperacoes.Visible = True
        LOperaçoes.Visible = True
        CBOperacoes.Visible = True
        LValor.Visible = True
        bAdicionar.Visible = True
    End Sub
#End Region
#Region "OPERACOES FALSE"
    Sub operacoesfalse()
        TBValOperacoes.Visible = False
        LOperaçoes.Visible = False
        CBOperacoes.Visible = False
        LValor.Visible = False
        bAdicionar.Visible = False
    End Sub
#End Region
#Region "READYONLY "
    Sub ReadyOnlyTrue()
        TCaixaInicial.ReadOnly = True
        TVendas.ReadOnly = True
        TSuplementos.ReadOnly = True
        TSangria.ReadOnly = True
        TValordoCAixa.ReadOnly = True
        TValFechamento.ReadOnly = True

    End Sub

#End Region
#Region "CARREGA COMBO"
    Sub CarregaCombo()
        objconexao.carregaCombo(CBOperacoes, "Generico.Gen_Status", "sta_codigo", "sta_descricao", "sta_codigo in (7,8)", "")
        objconexao.carregaCombo(CBStatusCx, "Generico.Gen_Status", "sta_codigo", "sta_descricao", "sta_codigo in (4,6)", "")
        objconexao.carregaCombo(CBUsuario, "Vendas.Gen_Usuario", "usu_codigo", "usu_login", "", "")
        CBUsuario.SelectedValue = -1
    End Sub
#End Region
#Region "LOAD"
    Private Sub CaixaFRM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        If (Op = "O") Then   'operações
            objBLL = New CaixaBLL(codigo)
            TSBAbrir.Visible = False
            operacoesfalse()
            LControlCaixa.Text = "Operações de Caixa"
            CBUsuario.Enabled = True
            ReadyOnlyTrue()
            TSBFechaCaixa.Select()
            CarregaCombo()
            CarregaCampos()
        Else : Op = "A"  'novo caixa
            objBLL = New CaixaBLL(codigo)
            operacoesfalse()
            LControlCaixa.Text = "Abertura de Caixa"
            CBUsuario.Enabled = True
            ReadyOnlyTrue()
            TSBSalvar.Visible = False
            abrirCaixa()
            CarregaCombo()
            objBLL.Limpar()
            CarregaPropriedades()
        End If
    End Sub
#End Region
#Region "ABRIR CAIXA"
    Private Sub TSBAbrir_Click(sender As System.Object, e As System.EventArgs) Handles TSBAbrir.Click
        Try
            abrirCaixa()
            TSBSalvar.Visible = True
            TSBAbrir.Visible = False
            TCaixaInicial.ReadOnly = False
            TCaixaInicial.Focus()
            LControlCaixa.Text = "Abertura de Caixa"
            TCaixaInicial.BackColor = Color.Yellow
        Catch ex As Exception

        End Try

    End Sub
#End Region
#Region "Abrir caixa"
    Sub abrirCaixa()
        TSBFechaCaixa.Visible = False
        TSBSangria.Visible = False
        TSBSuplemento.Visible = False
        CBStatusCx.SelectedValue = 4 'abertura
    End Sub
#End Region
#Region "Fechar Caixa"
    Sub FecharCaixa()
        operacoesfalse()
        TSBFechaCaixa.Visible = False
        TSBSalvar.Visible = True
        TSBAbrir.Visible = False
        TSBSangria.Visible = False
        TSBSuplemento.Visible = False
        TCaixaInicial.BackColor = Color.White
        TCaixaInicial.ReadOnly = True
        CBStatusCx.SelectedValue = 6
        LControlCaixa.Text = "Fechamento de Caixa"
        TValFechamento.ReadOnly = False
        TValFechamento.Text = ""
        TValFechamento.BackColor = Color.GreenYellow
        TValFechamento.Focus()
        Refresh()
    End Sub
#End Region
#Region "BOTÃO FECHAR"
    Private Sub TSBFechaCaixa_Click(sender As System.Object, e As System.EventArgs) Handles TSBFechaCaixa.Click
        FecharCaixa()
    End Sub
#End Region
#Region "Strip Button"
    Sub tsbFalse()
        TSBAbrir.Visible = False
        TSBSuplemento.Visible = False
        TSBSalvar.Visible = False
        TSBSangria.Visible = False
        TSBFechaCaixa.Visible = False
    End Sub
    Sub tsbTrue()
        TSBAbrir.Visible = False
        TSBSuplemento.Visible = True
        TSBSalvar.Visible = False
        TSBSangria.Visible = True
        TSBFechaCaixa.Visible = True
    End Sub
#End Region
#Region "BOTÃO SANGRIA"
    Private Sub TSBSangria_Click(sender As System.Object, e As System.EventArgs) Handles TSBSangria.Click
        CBOperacoes.SelectedValue = 8
        operacoesTrue()
        LControlCaixa.Text = "Sangria"
        tsbFalse()
        TBValOperacoes.ReadOnly = False
        TBValOperacoes.BackColor = Color.Yellow
        TBValOperacoes.Focus()
        Refresh()
    End Sub
#End Region
#Region "BOTAO SALVAR"
    Sub Salvar()
        CarregaPropriedades()
        objBLL.alterar()
        Limpar()
        MsgBox("Operação realizada com sucesso!", MsgBoxStyle.Information)
        Me.Close()
    End Sub


    Private Sub TSBSalvar_Click(sender As System.Object, e As System.EventArgs) Handles TSBSalvar.Click
        Dim opt As Integer
        Try
            If CBUsuario.SelectedValue <> Nothing Then
                CarregaPropriedades()
                Select Case CBStatusCx.SelectedValue()
                    Case 4 And (TValFechamento.Text = "") And Op = "O"
                        MsgBox("Digite um  valor de caixa válido!", MsgBoxStyle.Information)
                        TValFechamento.Select()
                    Case 6 And Op = "O" And (TValFechamento.Text = "")
                        MsgBox("Digite um  valor de caixa válido!", MsgBoxStyle.Information)
                        TValFechamento.Select()
                    Case 6 And (TValFechamento.Text = Decimal.Parse(TValordoCAixa.Text)) And Op = "O"
                        Salvar()
                    Case 6 And (TValFechamento.Text <> Decimal.Parse(TValordoCAixa.Text)) And Op = "O"
                        opt = MsgBox("Caixa não confere, deseja realmente fechar?", MsgBoxStyle.OkCancel)
                        TXTRes.Text = (Double.Parse(TValFechamento.Text) - Double.Parse(TValordoCAixa.Text))
                        If opt = 1 Then
                            Salvar()
                        Else
                            Me.Close()
                            objBLL = Nothing
                        End If
                    Case Else
                        Op = "A"
                        objBLL = New CaixaBLL(Val(TCodigo.Text), "Caixa 01", Date.Parse(DTPAbertura.Text), Nothing, Decimal.Parse(TCaixaInicial.Text.Replace("R$", "")), Decimal.Parse(TValordoCAixa.Text.Replace("R$", "")), Decimal.Parse(TValFechamento.Text), CBUsuario.SelectedValue, CBStatusCx.SelectedValue, Decimal.Parse(TXTRes.Text))
                        objBLL.inserir()
                        MsgBox("Operação realizada com sucesso!", MsgBoxStyle.Information)
                        Limpar()
                        Me.Close()
                End Select
            Else
                MsgBox("Selecione um usuário", MsgBoxStyle.Information)
                CBUsuario.Select()
            End If
        Catch ex As Exception
            MsgBox("Operação não realizada", MsgBoxStyle.Critical)
        End Try

    End Sub
#End Region
#Region "VALIDATED"
    Private Sub TBValOperacoes_Validated(sender As System.Object, e As System.EventArgs)
        TBValOperacoes.Text = FormatCurrency(TBValOperacoes.Text)
    End Sub
#End Region
#Region "KEYPRESS"
    Private Sub TBValOperacoes_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs)
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." And Not e.KeyChar = "," Then
            e.Handled = True
        End If
    End Sub

    Private Sub TCaixaInicial_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs)
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." And Not e.KeyChar = "," Then
            e.Handled = True
        End If
    End Sub

    Private Sub TValorFinal_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TValordoCAixa.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." And Not e.KeyChar = "," Then
            e.Handled = True
        End If
    End Sub

    Private Sub TSuprimentos_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TSuplementos.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." And Not e.KeyChar = "," Then
            e.Handled = True
        End If
    End Sub

    Private Sub TSangria_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TSangria.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." And Not e.KeyChar = "," Then
            e.Handled = True
        End If
    End Sub

    Private Sub TVendas_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TVendas.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." And Not e.KeyChar = "," Then
            e.Handled = True
        End If
    End Sub

    Private Sub TBvalFechamento_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TValFechamento.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." And Not e.KeyChar = "," Then
            e.Handled = True
        End If
    End Sub
#End Region
#Region "OUTROS VALIDATED"
    Private Sub TCaixaInicial_Validated(sender As System.Object, e As System.EventArgs)
        If TCaixaInicial.Text <> "" Then
            TCaixaInicial.Text = FormatCurrency(TCaixaInicial.Text)
        Else
            TCaixaInicial.Text = Nothing
        End If
    End Sub
    Private Sub TBvalFechamento_Validated(sender As System.Object, e As System.EventArgs) Handles TValFechamento.Validated
        If TValFechamento.Text <> "" Then
            TValFechamento.Text = FormatCurrency(TValFechamento.Text)
        Else
            TValFechamento.Text = Nothing
        End If
    End Sub

    Private Sub TSuprimentos_Validated(sender As System.Object, e As System.EventArgs) Handles TSuplementos.Validated
        If TSuplementos.Text <> "" Then
            TSuplementos.Text = FormatCurrency(TSuplementos.Text)
        Else
            TSuplementos.Text = Nothing
        End If
    End Sub
    Private Sub TVendas_Validated(sender As System.Object, e As System.EventArgs) Handles TVendas.Validated
        If TVendas.Text <> "" Then
            TVendas.Text = FormatCurrency(TVendas.Text)
        Else
            TVendas.Text = Nothing
        End If
    End Sub
#End Region
#Region "BOTÃO SUPLEMENTO"
    Private Sub TSBSuplemento_Click(sender As System.Object, e As System.EventArgs) Handles TSBSuplemento.Click
        CBOperacoes.SelectedValue = 7
        operacoesTrue()
        tsbFalse()
        LControlCaixa.Text = "Suplemento"
        TBValOperacoes.ReadOnly = False
        TBValOperacoes.Focus()
        Refresh()
    End Sub
#End Region
#Region "BOTÃO ADICIONAR"
    Private Sub bAdicionar_Click(sender As System.Object, e As System.EventArgs) Handles bAdicionar.Click
        If TBValOperacoes.Text <> 0 Then
            Select Case CBOperacoes.SelectedValue()
                Case 8 And (TBValOperacoes.Text > Decimal.Parse(TValordoCAixa.Text)) 'Sangria
                    MsgBox("Valor de Sangria maior que o permitido!", MsgBoxStyle.Exclamation)
                    TBValOperacoes.Text = ""
                    operacoesfalse()
                    TBValOperacoes.Focus()
                Case 7  ' Suplemento
                    objItBLL = New itensCaixaBLL(Val(TCodigo.Text), TBValOperacoes.Text, Date.Parse(DTPAbertura.Text), CBOperacoes.SelectedValue)
                    objItBLL.inserir()
                    TBValOperacoes.Text = ""
                    TCaixaInicial.BackColor = Color.White
                    MsgBox("Operação efetuada com sucesso!", MsgBoxStyle.Information)
                    objBLL.Limpar()
                    Close()
                Case Else
                    objItBLL = New itensCaixaBLL(Val(TCodigo.Text), TBValOperacoes.Text, Date.Parse(DTPAbertura.Text), CBOperacoes.SelectedValue)
                    objItBLL.inserir()
                    TBValOperacoes.Text = ""
                    TCaixaInicial.BackColor = Color.White
                    MsgBox("Operação efetuada com sucesso!", MsgBoxStyle.Information)
                    objBLL.Limpar()
                    Close()
            End Select
        Else
            MsgBox("Digite um valor válido", MsgBoxStyle.Information)
            TBValOperacoes.Focus()

        End If
    End Sub
#End Region
#Region "ALTERAR"
    Sub Alterar()
        Dim ret As Boolean
        objBLL.Limpar()
        CarregaPropriedades()
        If (ret = True) Then
            ret = objBLL.alterar()
            Limpar()
            TCaixaInicial.ReadOnly = True
            MsgBox("Operação realizada com sucesso!", MsgBoxStyle.Information)
            Close()
        Else
            MsgBox("Operação não realizada!", MsgBoxStyle.Critical)
        End If
    End Sub
#End Region
#Region "FECHAR"
    Private Sub TSBFechar_Click(sender As System.Object, e As System.EventArgs) Handles TSBFechar.Click
        If (TSBAbrir.Visible <> True And TSBFechaCaixa.Visible <> True And TSBSangria.Visible <> True And TSBSuplemento.Visible <> True And TSBSalvar.Visible <> True) Then
            TBValOperacoes.Text = ""
            operacoesfalse()
            LControlCaixa.Text = "Operações de Caixa"
            tsbTrue()
        Else
            objBLL.Limpar()
            LOperaçoes.Text = True
            objBLL = Nothing
            Limpar()
            Close()
        End If
    End Sub
#End Region
#Region "KEYPRESS & VALIDADED"
    Private Sub CBOperacoes_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles CBOperacoes.KeyPress
        e.Handled = True
    End Sub

    Private Sub TCaixaInicial_KeyPress_1(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TCaixaInicial.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." And Not e.KeyChar = "," Then
            e.Handled = True
        End If
    End Sub
    Private Sub TBValOperacoes_Validated_1(sender As System.Object, e As System.EventArgs) Handles TBValOperacoes.Validated
        If TBValOperacoes.Text <> "" Then
            TBValOperacoes.Text = FormatCurrency(TBValOperacoes.Text)
        Else
            TBValOperacoes.Text = Nothing
        End If
    End Sub

    Private Sub TCaixaInicial_Validated_1(sender As System.Object, e As System.EventArgs) Handles TCaixaInicial.Validated
        If TCaixaInicial.Text <> "" Then
            TCaixaInicial.Text = FormatCurrency(TCaixaInicial.Text)
        Else
            TCaixaInicial.Text = Nothing
        End If
    End Sub

    Private Sub TBValOperacoes_KeyPress_1(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TBValOperacoes.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." And Not e.KeyChar = "," Then
            e.Handled = True
        End If
    End Sub

    Private Sub TValordoCAixa_Validated(sender As System.Object, e As System.EventArgs) Handles TValordoCAixa.Validated
        If TValordoCAixa.Text <> "" Then
            TValordoCAixa.Text = FormatCurrency(TValordoCAixa.Text)
        Else
            TValordoCAixa.Text = Nothing
        End If
    End Sub
#End Region
#Region "Leave"
    Private Sub TSangria_Leave(sender As System.Object, e As System.EventArgs) Handles TSangria.Leave

    End Sub

    Private Sub TCaixaInicial_Leave(sender As System.Object, e As System.EventArgs) Handles TCaixaInicial.Leave

    End Sub

    Private Sub TSuplementos_Leave(sender As System.Object, e As System.EventArgs) Handles TSuplementos.Leave

    End Sub

    Private Sub TVendas_Leave(sender As System.Object, e As System.EventArgs) Handles TVendas.Leave

    End Sub

    Private Sub TValordoCAixa_Leave(sender As System.Object, e As System.EventArgs) Handles TValordoCAixa.Leave

    End Sub

    Private Sub TValFechamento_Leave(sender As System.Object, e As System.EventArgs) Handles TValFechamento.Leave

    End Sub

    Private Sub TXTRes_Leave(sender As System.Object, e As System.EventArgs) Handles TXTRes.Leave

    End Sub
#End Region

End Class